using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameForKids
{
    class BilliardStick
    {
        public String StickLink = "BilliardGame/Images/Stick";
        public Vector2 RootPosition;
        public Vector2 RootTemp;
        public Vector2 MovePosition;
        public Vector2 Strenght;
        public Vector2 MaxStrenght;
        public int TouchId;
        public bool OnTouch = false;
        public bool OnRepaire = false;
        public bool IsTranfer = false;
        public bool OnHit = false;
        public BilliardBall TargetBall;

        public float Rotation;
        public Vector2 CenterBall;
        public float BallWidth;

        public void Reset()
        {
            OnTouch = false;
            OnRepaire = false;
            Rotation = 0.0f;
        }

        public BilliardStick(float maxStrenght)
        {
            RootPosition = new Vector2();
            MovePosition = new Vector2();
            Strenght = new Vector2();
            MaxStrenght = new Vector2(maxStrenght, maxStrenght);
            Reset();
        }

        public void Update(GameTime gameTime)
        {
            if (OnHit == true)
            {
                Strenght *= 0.3f;
            }
        }

        public void Draw(SpriteBatch batch)
        {
            if (OnRepaire == true || OnHit == true)
            {
                batch.Draw(Suport.GetTexture(StickLink), RootTemp, null, new Color(0,0, 0, 0.2f), Rotation, new Vector2(0, 0.5f), 1.0f, SpriteEffects.None, 0.0f);
                batch.Draw(Suport.GetTexture(StickLink), RootPosition, null, Color.White, Rotation, new Vector2(0, 0.5f), 1.0f, SpriteEffects.None, 0.0f);
            }
        }

        public Vector2 GetTheDelta(Vector2 M, Vector2 K)
        {
            Vector2 n = new Vector2(K.X - M.X, K.Y - M.Y)/7;
            return n;
        }

        public Vector2 GetCutPoint(Vector2 I, float R, Vector2 K)
        {
            List<Vector2> points = new List<Vector2>();
            Vector2 n = new Vector2(K.X - I.X, K.Y - I.Y);
            //dt
            float a = n.X;
            float b = n.Y;
            float c = a * I.X + b * I.Y;
            // phuong trinh
            float c1 = (float)(-R * Math.Sqrt(a * a + b * b) - c);
            float c2 = (float)(R * Math.Sqrt(a * a + b * b) - c);
            float cTemp = c1;

            float IK = (float)Math.Sqrt((K.X - I.X) * (K.X - I.X) + (K.Y - I.X) * (K.Y - I.X));
            if (Math.Abs(IK - R - KhoangCach(a, b, c1, K)) < 0.01f)
            {
                cTemp = c1;
            }
            else if (Math.Abs(IK - R - KhoangCach(a, b, c2, K)) < 0.01f)
            {
                cTemp = c2;
            }
            System.Console.WriteLine("c = " + cTemp);

            // pt bat 2 voi c1: t1*x2 + t2*x + t3 = 0
            float t1 = (a * a) / (b * b) + 1;
            float t2 = -2 * I.X + 2 * a * cTemp / (b * b) + 2 * a * I.Y / b;
            float t3 = I.X * I.X + I.Y * I.Y - R * R + (cTemp * cTemp) / (b * b) + 2 * cTemp * I.Y / b;

            List<float> temp = PhuongTrinhB2(t1, t2, t3);
            System.Console.WriteLine(temp.Count);

            foreach (float nghiem in temp)
            {
                points.Add(new Vector2(nghiem, -(a / b) * nghiem - cTemp / b));
            }


            Vector2 p = new Vector2(0, 0);
            // tim diem
            foreach (Vector2 M in points)
            {
                //float MK = (float)Math.Sqrt((M.X - K.X) * (M.X - K.X) + (M.Y - K.Y) * (M.Y - K.Y));
                System.Console.WriteLine(M);
                //if (Math.Abs(IK - (MK + R)) < 0.01f)
                {
                    p += M;
                }
            }

            return (p/points.Count);
        }

        public List<float> PhuongTrinhB2(float a, float b, float c)
        {
            float deta = b * b - 4 * a * c;
            System.Console.WriteLine(a);
            System.Console.WriteLine(b);
            System.Console.WriteLine(c);
            System.Console.WriteLine(deta);

            List<float> nghiems = new List<float>();

                nghiems.Add((float)(-b) / (2 * a));
        
            return nghiems;
        }

        public float KhoangCach(float a, float b, float c, Vector2 point)
        {
            float len = (float)(Math.Abs(a * point.X + b * point.Y + c) / Math.Sqrt(a * a + b * b));
            return len;
        }
    }
}
